// pages/mine/mine.js
import Toast from '@vant/weapp/toast/toast';
const db = wx.cloud.database()
Page({
    /**
     * 页面的初始数据
     */
    data: {
      user: '',
      password: '',
      isLogin: true,
      loading: false,
      imgSrc: 'https://7869-xiaotu-1gkf9sf353dac377-1307545812.tcb.qcloud.la/3ea6beec64369c2642b92c6726f1epng.png?sign=59273e76f8e466eb68030d20814b5e3c&t=1712314020',
      nickname: '',
      type: '',
      dateText: ''
    },
    getTimeState(){
        // 获取当前时间
        let timeNow = new Date();
        // 获取当前小时
        let hours = timeNow.getHours();
        // 设置默认文字
        let text = ``;
        // 判断当前时间段
        if (hours >= 0 && hours <= 6) {
            text = `凌晨好`;
        } else if (hours > 6 && hours <= 11) {
            text = `上午好`;
        } else if (hours > 11 && hours <= 13) {
            text = `中午好`;
        } else if (hours > 13 && hours <= 18) {
            text = `下午好`;
        } else if (hours > 18 && hours <= 24) {
            text = `晚上好123`;
        }
        return text;
    },
    login(){
      if(!this.data.user || !this.data.password){
        Toast('请输入账号、密码');
        return
      }
      this.setData({
        loading: true
      })
      let that = this
      let data = {
        username: that.data.user,
        password: that.data.password
      }
      wx.cloud.callFunction({
        name: 'getOpenid',
        success(res){
          let loginDate = Date.now()
          let openid = res.result.openid
          db.collection('user').where(data).get().then(a=>{
            if(a.data.length != 0){
              db.collection('user').doc(a.data[0]._id).update({
                data: {
                  loginDate: openid + loginDate
                }
              }).then(e=>{
                Toast('登录成功');
                wx.setStorageSync('userInfo', JSON.stringify(a.data[0]))
                wx.setStorageSync('loginDate', openid + loginDate)
                setTimeout(()=>{
                  wx.reLaunch({
                    url: '/pages/mine/mine',
                    success:(res)=>{
                      that.setData({
                        loading: false
                      })
                    }
                  })
                },500)
              })
            }else {
              Toast('未注册');
            }
          }).catch(err=>{
            Toast('资源不足');
            that.setData({
              loading: false
            })
          })
        },
        fail(err){
          Toast('资源不足');
          that.setData({
            loading: false
          })
        }
      })
    },
    getUser(e){
      let that = this
      that.setData({
        user: e.detail.value
      })  
    },
    getPassword(e){
      let that = this
      that.setData({
        password: e.detail.value
      })  
    },
    goUserInfo(){
      wx.navigateTo({
        url: '/packageA/pages/userInfo/userInfo'
      })
    },
    goFirm() {
      wx.navigateTo({
        url: '/packageA/pages/companyInfo/companyInfo'
      })
    },
    goEmployee(){
      wx.navigateTo({
        url: '/packageA/pages/employee/employee'
      })
    },
    goSite(){
      wx.navigateTo({
        url: '/packageA/pages/site/site'
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
      
    },
   
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },
    exitClick(){
      wx.removeStorageSync('userInfo')
      wx.removeStorageSync('loginDate')
      wx.reLaunch({
        url: '/pages/mine/mine'
      })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      console.log('onShow');
      this.setData({
        dateText: this.getTimeState()
      })
      let that = this
      if(wx.getStorageSync('loginDate')){
        let userInfo = JSON.parse(wx.getStorageSync('userInfo'))
        that.setData({
          isLogin: false,
          imgSrc: userInfo.avater,
          nickname: userInfo.name,
          type: userInfo.type
        }) 
      }else {
        that.setData({
          isLogin: true
        }) 
      }
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})